package com.kinon.ceshiaccount.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kinon.ceshiaccount.R;
import com.kinon.ceshiaccount.bean.menu.Breakfast;
import com.kinon.ceshiaccount.util.ImageUtils;

import java.util.ArrayList;

/**
 * Created by lhqq on 2017-12-08.
 */

public class MenuAdapter extends BaseAdapter {

    private ArrayList<Breakfast> list;
    private Context mContext;
    private int mWidth=110;

    public MenuAdapter(ArrayList<Breakfast> breakfast,
                       Context mContext,int mWidth) {
        this.list = breakfast;
        this.mContext = mContext;
        this.mWidth=mWidth;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHandl handl;
        if(view==null){
            handl=new ViewHandl();
            view= LayoutInflater.from(mContext)
                    .inflate(R.layout.item_menu_layout,null);
            handl.fl_dishes= (FrameLayout) view.findViewById(R.id.fl_dishes);
            handl.rl_good= (RelativeLayout) view.findViewById(R.id.rl_good);
            handl.img_dishes= (ImageView) view.findViewById(R.id.img_dishes);
            handl.tv_price= (TextView) view.findViewById(R.id.tv_price);
            handl.tv_dishesName= (TextView) view.findViewById(R.id.tv_dishesName);
            handl.tv_num= (TextView) view.findViewById(R.id.tv_num);

            FrameLayout.LayoutParams fllp = new FrameLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            fllp.width=mWidth;
            fllp.height=mWidth;
            handl.fl_dishes.setLayoutParams(fllp);

            FrameLayout.LayoutParams flimglp =(FrameLayout.LayoutParams)
                    handl.img_dishes.getLayoutParams();
            flimglp.width=mWidth;
            flimglp.height=mWidth;
            handl.img_dishes.setLayoutParams(flimglp);

            view.setTag(handl);
        }else {
            handl = (ViewHandl) view.getTag();
        }
//        ImageLoaderManager.getInstance(mContext)
//                .displayImage(handl.img_dishes,list.get(i).getImage());
        ImageUtils.displayImage(mContext,list.get(i).getImage(),
                handl.img_dishes,R.drawable.img_imgloading);
        handl.tv_price.setText(list.get(i).getPrice()+"");
        handl.tv_dishesName.setText(list.get(i).getName());

        if(list.get(i).getAmount()==0){
            handl.tv_num.setVisibility(View.GONE);
        }else{
            handl.tv_num.setText(list.get(i).getAmount()+"");
            handl.tv_num.setVisibility(View.VISIBLE);
        }
//        handl.rl_good.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                handl.tv_num.setVisibility(View.VISIBLE);
//                handl.tv_num.setText("1");
//            }
//        });

        return view;
    }

    public void setmWidth(int mWidth) {
        this.mWidth = mWidth;
    }

    private class ViewHandl{
        FrameLayout fl_dishes;
        RelativeLayout rl_good;
        ImageView img_dishes;
        TextView tv_price;
        TextView tv_dishesName;
        TextView tv_num;
    }
}
